package com.demomaster.quickaccessibility_sample;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;

import androidx.annotation.Nullable;

import com.demomaster.quickaccessibility.receiver.ScreenStateReceiver;
import com.demomaster.quickaccessibility.sample.R;
import com.demomaster.quickaccessibility.service.FloatingMenuService;
import com.demomaster.quickaccessibility.service.HierarchyFlotingService;
import com.demomaster.quickcompat.service.ServiceHelper;

public class MainActivity extends Activity {

    Activity mContext = this;
    private static final int REQUEST_CODE_ACCESS_NOTIFICATION_POLICY = 1;
    ScreenStateReceiver screenStateReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_window).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //悬浮窗权限
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                startActivity(intent);
            }
        });
        findViewById(R.id.btn_window_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean exist = ServiceHelper.serverIsRunning(mContext, HierarchyFlotingService.class.getName());
                if (exist) {
                    ServiceHelper.stopService(mContext, HierarchyFlotingService.class);
                } else {
                    ServiceHelper.startService(mContext, HierarchyFlotingService.class);
                }
            }
        });
        findViewById(R.id.btn_window_floating_ball_menu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean exist = ServiceHelper.serverIsRunning(mContext, FloatingMenuService.class.getName());
                if (exist) {
                    ServiceHelper.stopService(mContext, FloatingMenuService.class);
                } else {
                    ServiceHelper.startService(mContext, FloatingMenuService.class);
                }
            }
        });

        findViewById(R.id.btn_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
                //QuickAccessibilityHelper.startSettintActivity(mContext);
                Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(intent);
            }
        });

        // 请求权限
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NOTIFICATION_POLICY)
//                != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(this,
//                    new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY},
//                    REQUEST_CODE_ACCESS_NOTIFICATION_POLICY);
//        } else {
        // 初始化并注册BroadcastReceiver
        screenStateReceiver = new ScreenStateReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(screenStateReceiver, filter);
        //}
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(screenStateReceiver);
    }
}
